package cn.iocoder.yudao.module.cmt.controller.admin.team;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.team.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.team.TeamApplyDO;
import cn.iocoder.yudao.module.cmt.service.team.TeamApplyService;

@Tag(name = "管理后台 - 团队申请")
@RestController
@RequestMapping("/cmt/team-apply")
@Validated
public class TeamApplyController {

    @Resource
    private TeamApplyService teamApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建团队申请")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:create')")
    public CommonResult<Long> createTeamApply(@Valid @RequestBody TeamApplySaveReqVO createReqVO) {
        return success(teamApplyService.createTeamApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新团队申请")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:update')")
    public CommonResult<Boolean> updateTeamApply(@Valid @RequestBody TeamApplySaveReqVO updateReqVO) {
        teamApplyService.updateTeamApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除团队申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:delete')")
    public CommonResult<Boolean> deleteTeamApply(@RequestParam("id") Long id) {
        teamApplyService.deleteTeamApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除团队申请")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:delete')")
    public CommonResult<Boolean> deleteTeamApplyList(@RequestParam("ids") List<Long> ids) {
        teamApplyService.deleteTeamApplyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得团队申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:query')")
    public CommonResult<TeamApplyRespVO> getTeamApply(@RequestParam("id") Long id) {
        TeamApplyDO teamApply = teamApplyService.getTeamApply(id);
        return success(BeanUtils.toBean(teamApply, TeamApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得团队申请分页")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:query')")
    public CommonResult<PageResult<TeamApplyRespVO>> getTeamApplyPage(@Valid TeamApplyPageReqVO pageReqVO) {
        PageResult<TeamApplyDO> pageResult = teamApplyService.getTeamApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeamApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出团队申请 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:team-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeamApplyExcel(@Valid TeamApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeamApplyDO> list = teamApplyService.getTeamApplyPage(pageReqVO).getList();
        ExcelUtils.write(response, "团队申请.xls", "数据", TeamApplyRespVO.class,
                        BeanUtils.toBean(list, TeamApplyRespVO.class));
    }

}
